import { RestHttpClient, BaseHttpClient } from "../../../http";
import { detectToken } from "../../../token";

export class GetHttpTraits {
    constructor(instance, task) {
        this.task = task;
    }
    private_data() {
        return {
            cache: true,
            response: []
        }
    }
    private_methods(exports) {
        var instance = this;
        exports.init = function () {
            exports.httpClient = new RestHttpClient();
            exports.httpClient.onLoad = function (...jsons) {
                var response = [];
                if (jsons[0]) {
                    exports.response = jsons[0];
                    instance.private_onLoad(exports);
                }
            };
        };

        exports.register = function (task) {
            exports.httpClient.register(task);
        }

        exports.send = function (params) {
            var self = this;
            if (exports.cache && exports.response.length > 0) {
                instance.private_onLoad(exports);
            } else {
                instance.private_send_http(exports, params);
            }
        };
    }

    private_handle(option, key) {
        return option;
    }

    private_onLoad(exports) {
        var self = this;
        var options = exports.response || [];
        if (options) {
            options = options.map(self.private_handle);
        }
        if (exports.onLoad) {
            exports.onLoad(options, exports.response);
        }
    }

    private_send_http(exports, params) {
        detectToken()
            .then(function (data) {
                var sendParams = Object.assign({
                    token: data.token
                }, params);
                exports.httpTask.setParams(sendParams);
                exports.httpClient.send();
            })
            .catch(function (error) {
            })
    };
}

export class HttpTaskTraits {
    constructor(instance, trait_name) {
        var self = this;
        return {
            _data() {
                var ret = {};
                ret[trait_name] = self.private_data();
                self.private_methods(ret[trait_name]);
                return ret;
            },
        }
    }

    private_data() {
        return {
            tasks: []
        }
    }

    private_methods(exports) {
        var instance = this;
        exports.init = function (init) {
            exports.httpClient = new BaseHttpClient();
            exports.httpClient.onLoad = function (...jsons) {
                instance.private_onLoad(exports, ...jsons);
            };
            if (init && init.onError) {
                exports.onError = init.onError.bind(exports)
            }
        };

        exports.register = function (task) {
            exports.tasks.push(task);
            exports.httpClient.register(task);
        }

        exports.clearStack = function () {
            exports.tasks = [];
            exports.httpClient.clearStack();
        }

        exports.send = function (params) {
            // var self = this;
            // if (exports.cache && exports.response.length > 0) {
            //     instance.private_onLoad(exports);
            // } else {
            //     instance.private_send_http(exports, params);
            // }

            instance.private_send_http(exports, params);
        };
    }

    private_onLoad(exports, ...jsons) {
        var self = this;
        exports.tasks.forEach(function (task) {
            if (task.onLoad) {
                task.onLoad();
            }
        });
        if (exports.onLoad) {
            exports.onLoad(...jsons);
        }
    }

    private_send_http(exports, globalparams) {
        detectToken()
            .then(function (data) {
                var sendParams = Object.assign({
                    token: data.token
                }, globalparams);

                exports.tasks.forEach(function (task) {
                    task.setParams(sendParams);                    
                })

                exports.httpClient.send();
            })
            .catch(function (error) {
                console.log(error);
                if (exports.onError) {
                    exports.onError(error)
                }
            })
    };
}